package grouplife::Controller::TuanZhuanYi;
use Moose;
use namespace::autoclean;
use Catalyst::Request::Upload;
use Catalyst::Log;
use grouplife::Model::Users::UsersSearchModel;
use grouplife::Model::Users::UserRegisterModel;
use grouplife::Model::News::MyNewsModel;
use grouplife::Model::Comments::CommentsForUsersModel;
use grouplife::Model::UserUser::UserUserModel;
use grouplife::Model::UserStore::UserStoreModel;
use grouplife::Model::Tuan::TuansSearchModel;
use grouplife::Model::CommentedItem::CommentedItemModel;
use grouplife::Model::University::UniversityModel;
use Util::Email::SendEmail;
use Digest::MD5;

BEGIN {extends 'Catalyst::Controller'; }
__PACKAGE__->config( 'Plugin::ConfigLoader' => { file => 'grouplife.yml' } );

=head1 NAME

grouplife::Controller::Users - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut

sub base :Chained('/') :PathPart('zhuanyi') :CaptureArgs(0) {
        my ($self, $c) = @_;
}

sub object :Chained('base') :PathPart('id') :CaptureArgs(1) {
        my ($self, $c, $id) = @_;
        $c->stash(object => $c->model('TuanZhuanYi::TuanZhuanYiModel')->find($id));
        $c->stash(id => $id);
}

sub zhuanyi_info :Chained('object') :PathPart('zhuanyi_info') :Args(0) {
	my ($self, $c) = @_;
	if (!$c->user_exists) {
                $c->response->redirect($c->uri_for('/login'));
                $c->flash->{redirect_after_login} = '' . $c->req->uri;
                return 0;
        }

        $c->cache_page( $c->config->{time_out} );

        my $request = $c->model('TuanZhuanYi::TuanZhuanYiModel')->find($c->stash->{object}->id);
	if ($c->session->{login_user}->id.'' ne $request->shangyou_user_id.'') {
		if ($c->session->{login_user}->id.'' ne $request->xiayou_user_id.'') {
			$c->stash(login_user => $c->session->{login_user});
                	$c->stash(css   => 'zhuce.css');
                	$c->stash(template => 'users/zhuanyi_info_failed.tt2');
			return 1;
		}
	}
	my $shangyou_user_id = $request->shangyou_user_id;
        my $shangyou_tuanzhang_info = $c->model('TuanZhang::TuanZhangModel')->tuanzhang_request_search($shangyou_user_id);
        my $shangyou_renzheng_status = 'U'; #'U'表示未提交申请
        if (defined $shangyou_tuanzhang_info) {
                $shangyou_renzheng_status = $shangyou_tuanzhang_info->status;
        	$c->stash(shangyou_tuanzhang_id => $shangyou_tuanzhang_info->id);
        }
	my $xiayou_user_id = $request->xiayou_user_id;
        my $xiayou_tuanzhang_info = $c->model('TuanZhang::TuanZhangModel')->tuanzhang_request_search($xiayou_user_id);
        my $xiayou_renzheng_status = 'U'; #'U'表示未提交申请
        if (defined $xiayou_tuanzhang_info) {
                $xiayou_renzheng_status = $xiayou_tuanzhang_info->status;
        }
        $c->stash(
		shangyou_renzheng_status => $shangyou_renzheng_status,
		xiayou_renzheng_status => $xiayou_renzheng_status,
                zhuanyi_request => $request,
		login_user => $c->session->{login_user},
		title => '拼单转移申请',
		css => 'zhuanyi_info.css',
                template => 'zhuanyi/zhuanyi_mainpage.tt2');
}

sub pizhun : Local {
	my ($self, $c) = @_;
	if (!$c->user_exists) {
                $c->response->redirect($c->uri_for('/login'));
                $c->flash->{redirect_after_login} = '' . $c->req->uri;
                return 0;
        }
	my $tzy_id = $c->request->params->{tzy_id};
	my $user_id = $c->session->{login_user}->id;
        my $request = $c->model('TuanZhuanYi::TuanZhuanYiModel')->find($tzy_id);
	if ($c->session->{login_user}->id.'' ne $request->shangyou_user_id.'') {
		$c->stash(login_user => $c->session->{login_user});
                $c->stash(css   => 'zhuce.css');
                $c->stash(template => 'users/zhuanyi_info_failed.tt2');
		return 1;
	}
	if ($request->req_status ne 'R') {
		my $message = "当前拼单转移请求已经被批准或拒绝过，无法再次批准";
		$c->stash(login_user => $c->session->{login_user});
                $c->stash(css   => 'zhuce.css');
                $c->stash(message => $message);
                $c->stash(request => $request);
                $c->stash(template => 'users/zhuanyi_status_failed.tt2');
		return 1;
	}
        $request = $c->model('TuanZhuanYi::TuanZhuanYiModel')->update_status($tzy_id,'Y',$user_id);
	my $sender = Util::Email::SendEmail->new();
        my $tzy_url = $c->config->{tzy_info_uri} . "/" . $request->id. "/zhuanyi_info";
        my (%params, %options);
        $options{INCLUDE_PATH} = $c->config->{email_template_path};
        $params{zhuanyi_request} = $request;
        $params{approve_time} = `date`;
        $params{conf_url} = $tzy_url;
        $params{username} = $request->xiayou_user_name;
        my $title = "【".$request->shangyou_tuan_name."】拼单转移申请已经被批准";
        my $template = "email/approveNotifyXiayouZhuanYi.html.tt";
	my $xiayouUser = $c->model('Users::UsersSearchModel')->find($request->xiayou_user_id);
        $sender->send($xiayouUser->email_address, $title, $template, \%params, \%options);
        $c->stash(
                zhuanyi_request => $request,
                #javascript => '/topic/discuss.js',
		login_user => $c->session->{login_user},
		title => '拼单转移申请',
		css => 'zhuce.css',
                template => 'zhuanyi/zhuanyi_mainpage.tt2');
}

sub jujue : Local {
	my ($self, $c) = @_;
	if (!$c->user_exists) {
                $c->response->redirect($c->uri_for('/login'));
                $c->flash->{redirect_after_login} = '' . $c->req->uri;
                return 0;
        }
	my $tzy_id = $c->request->params->{tzy_id};
	my $user_id = $c->session->{login_user}->id;
        my $request = $c->model('TuanZhuanYi::TuanZhuanYiModel')->find($tzy_id);
	if ($c->session->{login_user}->id ne $request->shangyou_user_id) {
		$c->stash(login_user => $c->session->{login_user});
                $c->stash(css   => 'zhuce.css');
                $c->stash(template => 'users/zhuanyi_info_failed.tt2');
		return 1;
	}
	if ($request->req_status ne 'R') {
		my $message = "当前拼单转移请求已经被批准或拒绝过，无法再次拒绝";
		$c->stash(login_user => $c->session->{login_user});
                $c->stash(css   => 'zhuce.css');
                $c->stash(message => $message);
                $c->stash(request => $request);
                $c->stash(template => 'users/zhuanyi_status_failed.tt2');
		return 1;
	}
        $request = $c->model('TuanZhuanYi::TuanZhuanYiModel')->update_status($tzy_id,'N',$user_id);
	my $sender = Util::Email::SendEmail->new();
        my $tzy_url = $c->config->{tzy_info_uri} . "/" . $request->id. "/zhuanyi_info";
        my (%params, %options);
        $options{INCLUDE_PATH} = $c->config->{email_template_path};
        $params{zhuanyi_request} = $request;
        $params{approve_time} = `date`;
        $params{conf_url} = $tzy_url;
        $params{username} = $request->xiayou_user_name;
        my $title = "【".$request->shangyou_tuan_name."】拼单转移申请被拒绝";
        my $template = "email/rejectNotifyXiayouZhuanYi.html.tt";
	my $xiayouUser = $c->model('Users::UsersSearchModel')->find($request->xiayou_user_id);
        $sender->send($xiayouUser->email_address, $title, $template, \%params, \%options);
        $c->stash(
                zhuanyi_request => $request,
                #javascript => '/topic/discuss.js',
		login_user => $c->session->{login_user},
		title => '拼单转移申请',
		css => 'zhuce.css',
                template => 'zhuanyi/zhuanyi_mainpage.tt2');
}

sub quxiao : Local {
	my ($self, $c) = @_;
	if (!$c->user_exists) {
                $c->response->redirect($c->uri_for('/login'));
                $c->flash->{redirect_after_login} = '' . $c->req->uri;
                return 0;
        }
	my $tzy_id = $c->request->params->{tzy_id};
	my $user_id = $c->session->{login_user}->id;
        my $request = $c->model('TuanZhuanYi::TuanZhuanYiModel')->find($tzy_id);
	if ($request->req_status ne 'R') {
		my $message = "当前拼单转移请求已经被上游营长处理，无法撤销";
		$c->stash(login_user => $c->session->{login_user});
                $c->stash(css   => 'zhuce.css');
                $c->stash(message => $message);
                $c->stash(request => $request);
                $c->stash(template => 'users/zhuanyi_status_failed.tt2');
		return 1;
	}
        $request = $c->model('TuanZhuanYi::TuanZhuanYiModel')->update_status($tzy_id,'C',$user_id);
	if ($c->session->{login_user}->id ne $request->xiayou_user_id) {
		$c->stash(login_user => $c->session->{login_user});
                $c->stash(css   => 'zhuce.css');
                $c->stash(template => 'users/zhuanyi_info_failed.tt2');
		return 1;
	}
        $c->stash(
                zhuanyi_request => $request,
                #javascript => '/topic/discuss.js',
		login_user => $c->session->{login_user},
		title => '拼单转移申请',
		css => 'zhuce.css',
                template => 'zhuanyi/zhuanyi_mainpage.tt2');
}

sub fuzhi_tuan : Local {
	my ($self, $c) = @_;
	if (!$c->user_exists) {
                $c->response->redirect($c->uri_for('/login'));
                $c->flash->{redirect_after_login} = '' . $c->req->uri;
                return 0;
        }
	my $tzy_id = $c->request->params->{tzy_id};
        my $request = $c->model('TuanZhuanYi::TuanZhuanYiModel')->find($tzy_id);
	if ($c->session->{login_user}->id ne $request->xiayou_user_id) {
		$c->stash(login_user => $c->session->{login_user});
                $c->stash(css   => 'zhuce.css');
                $c->stash(template => 'users/zhuanyi_info_failed.tt2');
		return 1;
	}
	if ($request->req_status eq 'Y' or $request->req_status eq 'Z') {
	my $shangyou_tuan_id = $request->shangyou_tuan_id;
	my $tuan = $c->model('Tuan::TuansSearchModel')->find($shangyou_tuan_id);
        my $user = $c->model('Users::UsersSearchModel')->find($c->session->{login_user}->id);
        my $shangyou_user = $c->model('Users::UsersSearchModel')->find($request->shangyou_user_id);
        my $university = $c->model('University::UniversityModel')->find($request->zhuanyi_univ_id);

        my @tuan_stores = $tuan->tuan_stores;
	my @stores;
	foreach my $ts(@tuan_stores) {
		my $store_id = $ts->stores_id;
		my $store = $c->model('Store::StoreSearchModel')->find($store_id);
		push(@stores, $store);
	}
	
	my $description = $tuan->description;
	$description =~ tr/[\r\n]//d;

	my $curr_time = `date --iso-8601=seconds`;
        $c->stash(login_user => $c->session->{login_user},
		tzy_id => $tzy_id,
                javascript => '/store/create_tuan.js',
                css => 'create_new_tuan.css',
                university=>$university,
                curr_time => $curr_time,
                user => $user,
                tuan => $tuan,
                description => $description,
                tuan_stores => \@stores,
                template => "stores/fuzhi_tuan.tt2"
		);
	}
	else {
		my $message = "当前拼单转移请求没有被批准或者下游拼单已经创建";
		$c->stash(login_user => $c->session->{login_user});
                $c->stash(css   => 'zhuce.css');
                $c->stash(message => $message);
                $c->stash(request => $request);
                $c->stash(template => 'users/zhuanyi_status_failed.tt2');
		return 1;
	}
}

=head1 AUTHOR

lijie,,,

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

1;
